﻿var FocusManager =
{
    controlonfocus : "",
    disposeFunctions : [],
    
    registerControl : function(disposeFunction)
    {
	    FocusManager.disposeFunctions.push(disposeFunction);
    },
        
    setFocus : function(id)
    {
	    if (id == FocusManager.controlonfocus) return;

	    FocusManager.sendToBack(FocusManager.controlonfocus);
	    FocusManager.bringToFront(id);	

        if(FocusManager.controlonfocus!="")
        {
            for(var i=0; i<FocusManager.disposeFunctions.length; i++) {
                eval(FocusManager.disposeFunctions[i] + "(\"" + FocusManager.controlonfocus + "\")")
            }
        }
            	
	    FocusManager.controlonfocus = id;
    },

    disposeFocus : function(ev)
    {
        ev = ev || window.event;
        
        if(!Common.getSrcElement(ev).getAttribute)
            return;
            
	    if (Common.getSrcElement(ev).getAttribute("list")) return;
	    if (Common.getSrcElement(ev).getAttribute("selector")) return;
	    if (Common.getSrcElement(ev).getAttribute("datePicker")) return;	    
	    if (Common.getSrcElement(ev).getAttribute("calendar")) return;	    
	    if (Common.getSrcElement(ev).getAttribute("checklist")) return;	    
	    if (FocusManager.controlonfocus=="") return;

	    FocusManager.sendToBack(FocusManager.controlonfocus);
        
        if(FocusManager.controlonfocus!="")
        {
            for(var i=0; i<FocusManager.disposeFunctions.length; i++) {
                eval(FocusManager.disposeFunctions[i] + "(\"" + FocusManager.controlonfocus + "\")")
            }
        }
        	
	    FocusManager.controlonfocus = "";
    },

    isFocused : function(obj)
    {
	    return(obj.id == FocusManager.controlonfocus)
    },

    bringToFront : function(id)
    {
        var cont = document.getElementById("divcont" + id);
        Common.bringToFront(cont, false);
    },

    sendToBack : function(id)
    {
	    var cont = document.getElementById("divcont" + id);
        Common.sendToBack(cont);
    }
}

// Common.addEventListener(window, "mousedown", FocusManager.disposeFocus, false);
Common.addEventListener(document, "mousedown", FocusManager.disposeFocus, false);
